#pragma once
#include <iostream>
#include <functional>
#include <string>
#include <pthread.h>
#include <string.h>
namespace mystl
{
    using task_t = std::function<void()>;
    class thread
    {
    private:
        static void* Exec(void* args)
        {
            thread* self = static_cast<thread*>(args);
            if(self->_task != nullptr)
            {
                self->_task();
            }
        }
    public:
        thread(const thread& tid) = delete;
        explicit thread(const task_t& task)
            :_task(task)
        {
            //先创建线程
            int n = ::pthread_create(&_tid,nullptr,Exec,this);
            if( n != 0)
            {
                std::cerr<<std::string("create thread failed! reason: " ) + std::string(strerror(errno))<<std::endl;
                return;
            }
        }
        void detach()
        {
            int n = ::pthread_detach(_tid);
            //判断逻辑TODO
        }
        void join()
        {
            int n = ::pthread_join(_tid,nullptr);
        }
        static uint64_t ThreadID()
        {
            return ::pthread_self();
        }
    private:
        pthread_t _tid;
        task_t _task;
    };
}